package com.yang.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.sun.deploy.net.HttpUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.io.IOException;
import java.util.HashMap;

/**
 * 企业微信通知工具类
 *
 * @author LJM
 * @package com.zbkj.service.util
 * @createTime 2025-01-24 13:53
 */
@Slf4j
@Component
public class WeComNoticeUtil {

    private static final Logger LOGGER = LoggerFactory.getLogger(WeComNoticeUtil.class);

    @Value("${weCom.notice.url:}")
    private String noticeUrlClass;

    private static String noticeUrl;

    @PostConstruct
    public void init(){
        WeComNoticeUtil.noticeUrl = noticeUrlClass;
    }

    public static void send(String content) {
        if (noticeUrl == null || noticeUrl.isEmpty()){
            return;
        }
        new Thread(() -> {
            try {
                HashMap<String, Object> markdownMap = new HashMap<>();
                String requestContent = content.length() > 4096 ? content.substring(0, 4000) : content;
                markdownMap.put("content", requestContent);
                HashMap<String, Object> paramMap = new HashMap<>();
                paramMap.put("msgtype", "markdown");
                paramMap.put("markdown", markdownMap);
                String rep = doPostJson(noticeUrl, JSON.toJSONString(paramMap));
                LOGGER.info("异常日志发送企业微信通知结果:" + rep);
            }catch (Exception e){
                LOGGER.error("异常日志发送企业微信通知HTTP调用异常:{}", e.getMessage(), e);
            }
        }).start();
    }

    public static String doPostJson(String url, String json) {
        // 创建Httpclient对象
        CloseableHttpClient httpClient = HttpClients.createDefault();
        CloseableHttpResponse response = null;
        String resultString = "";
        try {
            // 创建Http Post请求
            HttpPost httpPost = new HttpPost(url);
            // 创建请求内容
            StringEntity entity = new StringEntity(json, ContentType.APPLICATION_JSON);
            httpPost.setEntity(entity);
            // 执行http请求
            response = httpClient.execute(httpPost);
            resultString = EntityUtils.toString(response.getEntity(), "utf-8");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                response.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return resultString;
    }
}
